package org.dreamfly.dreamalarm.task;

import org.dreamfly.dreamalarm.bean.Alarm;
import org.dreamfly.dreamalarm.inter.DeleteComponent;
import org.dreamfly.dreamalarm.util.db.DataBaseHelper;

import android.content.Context;
import android.database.Cursor;

import com.github.droidfu.concurrent.BetterAsyncTask;

public class DeleteAsyncTask extends BetterAsyncTask<String, Void, String> {

	DeleteComponent component;
	int id;
	boolean isTrunOn;

	public DeleteAsyncTask(Context arg0, DeleteComponent component,
			int id, Boolean needDialog) {
		super(arg0);
		// TODO Auto-generated constructor stub

		this.component = component;
		this.id=id;
		if (!needDialog) {
			super.disableDialog();
		}
	}

	// 处理删除内容
	@Override
	protected String doCheckedInBackground(Context context, String... obj)
			throws Exception {
		super.doCheckedInBackground(context, obj);

		Cursor cursor=DataBaseHelper.getInstance().findById("alarm", id+"");
		cursor.moveToFirst();
		isTrunOn=(cursor.getInt(cursor.getColumnIndex("state"))==(Alarm.TURN_ON));
		cursor.close();
		
		DataBaseHelper.getInstance().deleteAlarm(id);

		return "";
	}

	@Override
	protected void after(Context arg0, String arg1) {
		// TODO Auto-generated method stub
		component.deleteSuccessRefresh(id,isTrunOn);
	}

	@Override
	protected void handleError(Context arg0, Exception arg1) {
		// TODO Auto-generated method stub
		component.deleteErrorRefresh();
	}
}
